package com.dnxh.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dnxh.constant.CustomResult;
import com.dnxh.entity.BasicInfo;
import com.dnxh.entity.common.PageRequest;
import com.dnxh.entity.request.CreateBasicInfoRequest;
import com.dnxh.entity.request.SearchBasicInfoRequest;
import com.dnxh.service.BasicInfoService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhiqiu
 * @since 2022 -04-06
 */
@RestController
@RequestMapping ("/info")
public class BasicInfoController {
	
	@Resource
	private BasicInfoService basicInfoService;
	
	/**
	 * 创建一个人员
	 *
	 * @param request the request
	 * @return the custom result
	 */
	@PostMapping ("/create")
	public CustomResult create (@RequestBody @Valid CreateBasicInfoRequest request) {
		basicInfoService.create (request);
		return CustomResult.success ();
	}
	
	/**
	 * 确认/取消成员
	 *
	 * @param id the id
	 * @return the custom result
	 */
	@PutMapping ("/confirm/{id}")
	public CustomResult confirm (@PathVariable ("id") Long id) {
		return basicInfoService.confirm (id);
	}
	
	/**
	 * 查询基本信息
	 *
	 * @param req the req
	 * @return the custom result
	 */
	@PostMapping ("/search")
	public CustomResult searchInfo (@Valid @RequestBody SearchBasicInfoRequest req) {
		return basicInfoService.searchInfo (req);
	}
	
	/**
	 * 查询部门信息
	 *
	 * @param req the req
	 * @return the custom result
	 */
	@PostMapping ("/search/department")
	public CustomResult searchDepartment (@RequestBody @Valid PageRequest req) {
		return basicInfoService.searchDepartment (req);
	}
	
	/**
	 * 设置用户部门
	 *
	 * @param id           the id
	 * @param departmentId the department id
	 * @return the department
	 */
	@PutMapping("/set/department")
	public CustomResult setDepartment(@RequestParam("id") Long id,@RequestParam("departmentId") Long departmentId){
		basicInfoService.update (Wrappers.<BasicInfo>lambdaUpdate ()
				.set (BasicInfo::getDepartment,departmentId)
				.eq (BasicInfo::getId,id));
		return CustomResult.success ();
	}
}

